package com.ayit.controller;

import com.ayit.pojo.Category;
import com.ayit.pojo.Result;
import com.ayit.service.CategoryService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //添加分类
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category){
        categoryService.add(category);
        return Result.success("添加分类成功");
    }

    //获取分类列表
    @GetMapping
    public Result<List<Category>> list(){//List是Java里的集合，里面是多个Category对象
        List<Category> cs = categoryService.list();
        return  Result.success(cs);
    }

    //获取文章分类详情
    @GetMapping("/detail")
    public Result<Category> detail(Integer cid){//cid是前端传回来的文章排序id
        Category c = categoryService.findById(cid);
        return Result.success(c);
    }

    //更新文章分类详情
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category){
        categoryService.update(category);
        return Result.success("更新成功 ！");
    }

    //删除分类
    @DeleteMapping()
    public Result delete(Integer id){

/*        Category c = categoryService.findById(id);
        if(c == null){
            return Result.error("要删除的分类不存在！ ");
        }*/
        categoryService.delete(id);
        return Result.success("删除成功！");
    }
}
